package gulimall.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import gulimall.entity.Brand;
import gulimall.service.IBrandService;
import gulimall.utils.PageUtils;
import gulimall.utils.R;
import gulimall.valid.AddGroup;
import gulimall.valid.UpdateGroup;
import gulimall.valid.UpdateShowStatusGroup;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 品牌
 *
 * @author HLF
 * @email 1132814637@qq.com
 * @date 2022-04-29 18:10:58
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController {
    @Resource
    private IBrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimall-product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("gulimall-product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        Brand brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * 使用@Valid注解标注的字段需要进行校验
     * 使用@Validated注解标注的字段需要进行分组校验
     * BindingResult 错误信息
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimall-product:brand:save")
    //public R save(@Valid @RequestBody Brand brand /* , BindingResult bindingResult */) {
    public R save(@Validated(value = {AddGroup.class}) @RequestBody Brand brand) {
        /*Map<String, String> map = new HashMap<>();
        //获取校验的错误结果
        if (bindingResult.hasErrors()) {
            bindingResult.getFieldErrors().forEach(fieldError -> {
                //错误提示
                String defaultMessage = fieldError.getDefaultMessage();
                //发生错误的字段(属性)
                String field = fieldError.getField();
                map.put(field, defaultMessage);
            });
            return R.error(400, "提交的数据不合法！").put("data", map);
        } else {
            brandService.save(brand);
            return R.ok();
        }*/

        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimall-product:brand:update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody Brand brand) {
        //brandService.updateById(brand);//只更改品牌表自己

        //由于没有外键，所以需要改变品牌表和其他引用品牌表的字段的地方
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改显示状态
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("gulimall-product:brand:update")
    public R updateShowStatus(@Validated(value = {UpdateShowStatusGroup.class})
                              @RequestBody Brand brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimall-product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
